//
//  SearchUserViewController.m
//  项目三
//
//  Created by rimi1 on 2016/12/14.
//  Copyright © 2016年 Wangxingqian. All rights reserved.
//

#import "SearchUserViewController.h"
#import "OtherPersonalViewController.h"

#import "NotView.h"
#import "SearchUserTableViewCell.h"

#import "AVRequest.h"

@interface SearchUserViewController () <UITableViewDelegate, UITableViewDataSource, UIScrollViewDelegate>

@property (nonatomic, strong) UITextField *searchBar;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray <AVObject *> *dataArr;
@property (nonatomic, strong) NotView *notView;

@end

@implementation SearchUserViewController

- (void)viewDidAppear:(BOOL)animated {
    [self.searchBar becomeFirstResponder];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initializeDataSource];
    [self initializeAppearance];
}

#pragma mark - initialize

- (void)initializeDataSource {
    self.dataArr = [NSMutableArray array];
}

- (void)initializeAppearance {
    [self.navigationBar.right setImage:[UIImage imageNamed:@"搜索"] forState:UIControlStateNormal];
    
    [self.navigationBar addSubview:self.searchBar];
    [self.view addSubview:self.tableView];
}

#pragma mark - URLRequest

- (void)urlRequest {
    NSString *str = self.searchBar.text;
    AVQuery *name = [AVQuery queryWithClassName:@"MyUser"];
    AVQuery *account = [AVQuery queryWithClassName:@"MyUser"];
    [name whereKey:@"name" containsString:str];
    [account whereKey:@"account" containsString:str];
    
    AVQuery *query = [AVQuery orQueryWithSubqueries:@[name, account]];
    
    [AVRequest avQueryWithQuery:query queryArr:^(NSArray *objects, NSError *error) {
        if (error) {
            self.notView.title.text = @"搜索出了点问题，重新再搜索一次吧...";
            [self.view addSubview:self.notView];
            return;
        }
        
        if (objects.count != 0) {
            [self.notView removeFromSuperview];
            [self.dataArr addObjectsFromArray:objects];
            [self.tableView reloadData];
        }else {
            self.notView.title.text = @"您搜索的内容太酷，目前没有符合的...";
            [self.view addSubview:self.notView];
        }
        
    }];
}

#pragma mark - respondsTo..

- (void)respondsToRight:(UIButton *)sender {
    if (self.searchBar.text.length != 0) {
        [self urlRequest];
    }
}

- (void)respondsToTap {
    [self.searchBar resignFirstResponder];
}

#pragma mark - UIScrollViewDelegate

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    [self.searchBar resignFirstResponder];
}


#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *reusing = @"SearchUserCell";
    SearchUserTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reusing];
    if (!cell) {
        cell = [[SearchUserTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reusing];
    }
    AVObject *obj = self.dataArr[indexPath.row];
    
    NSURL *url = [NSURL URLWithString:obj[@"headImage"]];
    [cell.headImage sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"个人"] options:SDWebImageProgressiveDownload];
    cell.name.text = obj[@"name"];
    cell.words.text = [NSString stringWithFormat:@"%@个作品", obj[@"myDynamicCount"]];
    
    return cell;
}

#pragma mark - UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self.searchBar resignFirstResponder];
    [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    AVObject *obj = self.dataArr[indexPath.row];
    OtherPersonalViewController *personal = [[OtherPersonalViewController alloc] init];
    personal.obj = obj;
    [self.navigationController pushViewController:personal animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 40;
}

#pragma mark - get

- (UITextField *)searchBar {
    
    if (!_searchBar) {
        
        CGFloat x = CGRectGetMaxX(self.navigationBar.left.frame);
        _searchBar = [[UITextField alloc] initWithFrame:CGRectMake(x, 20, kWIDTH - x - 20, 40)];
        _searchBar.rightView = self.navigationBar.right;
        _searchBar.rightViewMode = UITextFieldViewModeAlways;
        _searchBar.placeholder = @"输入大厨的名字/账号";
        
        UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, _searchBar.bounds.size.height - 4, _searchBar.bounds.size.width, 2)];
        line.backgroundColor = [UIColor lightGrayColor];
        [_searchBar addSubview:line];
        
    }
    return _searchBar;
}

- (UITableView *)tableView {
    
    if (!_tableView) {
        
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, kWIDTH, kHEIGHT - 64) style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.tableFooterView = [UIView new];
        
    }
    return _tableView;
}

- (NotView *)notView {
    
    if (!_notView) {
        
        _notView = [[NotView alloc] initWithFrame:self.tableView.frame];
        _notView.backgroundColor = [UIColor whiteColor];
        [_notView.refresh removeFromSuperview];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(respondsToTap)];
        [_notView addGestureRecognizer:tap];
        
    }
    return _notView;
}

@end














